package io.sage.modules.goods.service.impl;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.sage.modules.basics.dao.BGoodsAllocationMapper;
import io.sage.modules.goods.entity.BGoodsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.sage.modules.goods.dao.BGoodsMapper;
import io.sage.modules.goods.entity.BGoods;
import io.sage.modules.goods.service.BGoodsService;

@Service("goodsService")
public class BGoodsServiceImpl implements BGoodsService {

	@Autowired
	private BGoodsMapper goodsMapper;
	@Autowired
	private BGoodsAllocationMapper bGoodsAllocationMapper;

	/**
	 * 货物列表
	 * @param example
	 * @return
	 */
	@Override
	public List<BGoods> selectByExample(BGoodsExample example) {
		return goodsMapper.selectByExample(example);
	}

	/**
	 * 根据ID修改状态，当前example只用于where条件
	 *
	 * @param list
	 * @return
	 */
	@Override
	public int updateStatusByIds(List<Integer> list) {
		BGoodsExample bGoodsExample = new BGoodsExample();
		BGoodsExample.Criteria criteria = bGoodsExample.createCriteria();
		criteria.andIdIn(list);
		return goodsMapper.updateStatusByIds(bGoodsExample);
	}

	/**
	 * 货物列表
	 * @param params
	 * @return
	 */
	@Override
	public List<BGoods> selectGoodsList(Map<String, Object> params) {
		return goodsMapper.selectGoodsList(params);
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@Override
	public int deleteBatchIds(Integer[] ids) {
		return goodsMapper.deleteBatchIds(ids);
	}

	/**
	 * 根据主键id查询货物详情
	 * @param id
	 * @return
	 */
	@Override
	public BGoods selectByPrimaryKey(Integer id) {
		return goodsMapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据主键修改货物信息
	 * @param goods
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(BGoods goods) {
		goods.setUpdateTime(new Date());
		if(goods.getDueDate()==null&&goods.getCheckTime()!=null&&goods.getCheckTime()>0){
			goods.setDueDate(this.getAfterDate(goods.getCreateTime(),goods.getCheckTime() ));
		}
		int i=goodsMapper.updateByPrimaryKeySelective(goods);
		if(i>0){
			this.bGoodsAllocationMapper.updateIsUsedById(goods.getAllocationId());
		}
		return i;
	}

	/**
	 * 添加货物信息
	 * @param goods
	 * @return
	 */
	@Override
	public int insert(BGoods goods) {
		goods.setCreateTime(new Date());
		goods.setUpdateTime(new Date());
		goods.setIsDel(0);
		goods.setIsUsed(1);
		goods.setGoodsStatus(0);
		if(goods.getDueDate()==null&&goods.getCheckTime()!=null&&goods.getCheckTime()>0){
			goods.setDueDate(this.getAfterDate(goods.getCreateTime(),goods.getCheckTime() ));
		}
		int i=goodsMapper.insertSelective(goods);
		if(i>0){
			this.bGoodsAllocationMapper.updateIsUsedById(goods.getAllocationId());
		}
		return i;
	}

	@Override
	public int updateUpShelfBatch(List<BGoods> list) {

		return goodsMapper.updateUpShelfBatch(list);
	}

	@Override
	public Integer selectNewCount( Map<String, Object> params) {
		return goodsMapper.selectNewCount(params);
	}

	@Override
	public void batchUpdtaeGoodsStatus() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();

	}


	public Date getAfterDate(Date date,Integer num){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
// add方法中的第二个参数n中，正数表示该日期后n天，负数表示该日期的前n天
		calendar.add(Calendar.DATE, num);
		return calendar.getTime();
	}

}
